<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/admin/index' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>老人健康信息列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
<!--    <el-button type="primary" icon="el-icon-edit" style="margin-left: 30px" @click="addNew()">添加</el-button>-->
    <el-button type="warning" icon="el-icon-edit" @click="addNew()">添加老人</el-button>
<!--    <el-button type="warning" icon="el-icon-delete"  @click="deleteByIds()">删除</el-button>-->
<!--    <el-table id ="selectTable" :data="tableData" style="overflow: scroll;height: 650px;margin-top: 5px" @selection-change="handleSelectionChange()">-->
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%;height: 650px;overflow: scroll;"
        @selection-change="handleSelectionChange()">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" :index="indexMethod" label="编号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="phoneNumber" label="手机号码"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="birth" label="生日" ></el-table-column>
      <el-table-column prop="height" label="身高"></el-table-column>
      <el-table-column prop="weight" label="体重"></el-table-column>
      <el-table-column prop="bp" label="舒张压(mm)"></el-table-column>
      <el-table-column prop="bg" label="血糖(毫摩尔/升)"></el-table-column>
      <el-table-column prop="bl" label="血脂(毫摩尔/升)"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="warning"
              @click="openEditDialog(scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="info"
              @click="openDeleteConfirm(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div class="block" style="position: absolute;bottom:20px;left: 40%;">
      <el-pagination background
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="pageForm.page"
                     :page-sizes="[10,20,30,40,50,60]"
                     :page-size="pageForm.pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id:"",
        name: '测试名称001',
        gender:'男',
        age:'88',
        phoneNumber:'145789',
        birth:"",
        height: 158,
        weight: 45,
        bp: 74.2,
        bg: 74.3,
        bl: 74.5
      },
      pageForm:{
        page: 1,
        pageSize: 10
      },
      formLabelWidth: '120px',
      multipleSelection: [],
      Page: '',
      pageSize : '',
      total: ""
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /*submitEditForm() {
      let url = 'http://localhost:8086/albums/' + this.editForm.id + '/update';
      console.log("url = " + url);
      let  formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization' : localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible  = false;
          this.$message({
            message: '修改相册信息成功! ',
            type: 'success'
          });
          this.loadAlbumList();
        }else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      });
    },*/

    addNew() {
      location.href="/admin/older/add-new";
    },
    openEditDialog(older) {
      /*let url = 'http://localhost:8080/olders/'+ older.id;
      console.log('url = ' + url);

      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        }else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOlderList();
            }
          });
        }
      });*/
      location.href = '/admin/older/update?id=' + older.id;
    },
    openDeleteConfirm(older) {
      let message = '此操作将永久删除【'+ older.name +'】老人, 是否继续?'
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(older);
      }).catch(() => {

      });
    },
    handleDelete(older) {
      let url = 'http://localhost:8080/olders/'+ older.id +'/delete';
      console.log("url = " + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadOlderList();
        }else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOlderList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadOlderList() {
      /*let url = "http://localhost:8080/olders";*/
      let url = "http://localhost:8080/olders/list-by-page?"+this.qs.stringify(this.pageForm);
      console.log("url = " + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.total = responseBody.data.total;
          this.tableData = responseBody.data.list;

        }
      });
    },
    handleSizeChange(val) {
      this.pageForm.pageSize = val;
      this.loadOlderList();
    },
    handleCurrentChange(val) {
      this.pageForm.page = val;
      console.log(val);
      this.loadOlderList();
    },
    indexMethod(index) {

      return (this.pageForm.page-1)*this.pageForm.pageSize+index+1;
    }
  },
  mounted() {
    this.loadOlderList();
  }
}
</script>
